import {defineStore} from "pinia";
import http from "../utils/http"
export const useFiles = defineStore("files",{
     state:()=>{
        return {
            //文件数据
            fileLists:[],
            totals:0,
            //海报数据
            posterList:[],
            cover:""
        }
     },
     actions:{
        async getsearchfiles(obj){
            let res = await http.get("/api/file",{params:obj});
            // console.log(res.data.data[0],"file res")
            if(res.status===200){
                //处理后端发过来的数据
                let newarr = res.data.data[0].map(v=>{
                    v.createAt = new Date(v.createAt).toLocaleString()
                    return v
                })
                this.fileLists = newarr
                this.totals = res.data.data[1] 
            }
        },
        //上传图片
        async uploadfile(file){
            let files = new FormData()
            files.append("file",file)
            // console.log(files,"files")
            let res = await http.post("/api/file/upload?unique=0",files)
            // console.log(res,"upload res")
            if(res.status == 201){
                this.getsearchfiles({page:1,pageSize:12})
                this.cover = res.data.data.url
            }
            
        },
        //获取海报数据
        async getposter(obj){
            let res = await http.get("/api/poster",{params:obj})
            // console.log(res,"poster")
            if(res.status===200){
                //处理后端发过来的数据
                let newarr = res.data.data[0].map(v=>{
                    v.createAt = new Date(v.createAt).toLocaleString()
                    return v
                })
                this.posterList = newarr
                this.total = res.data.data[1] 
            }
        }
     },
     getters:{

     }
})